<?php

namespace addons\qingdongams\controller;

use addons\qingdongams\library\QRCode as QrcodeModel;

use think\Response;

/**
 * 公共接口
 */
class Qrcode extends StaffApi {
	protected $noNeedLogin = ['qrcode'];
	protected $noNeedRight = '*';
    /**
     * 生成二维码
     */
    public function qrcode()
    {
        $text = $this->request->get('text', 'hello world');
        //如果有安装二维码插件，则调用插件的生成方法
        if (class_exists("\addons\qrcode\library\Service") && get_addon_info('qrcode')['state']) {
            $qrCode = \addons\qrcode\library\Service::qrcode(['text' => $text]);
            $response = Response::create()->header("Content-Type", "image/png");

            header('Content-Type: ' . $qrCode->getContentType());
            $response->content($qrCode->writeString());
            return $response;
        } else {
            header("Content-type: image/png");
            $qr = QrcodeModel::getMinimumQRCode($text);
            $im = $qr->createImage(8, 5);
            imagepng($im);
            imagedestroy($im);
            return;
        }
    }

}
